package com.huaxindata.im.main.chat.Helper;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huaxin.common.message.util.FileUtil;
import com.huaxin.common.utils.Util;
import com.huaxindata.im.R;
import com.huaxindata.im.main.chat.adapter.ChatAdapter;
import com.huaxindata.im.model.entity.ChatImageMessageBody;
import com.huaxindata.im.model.entity.ChatMessage;
import com.huaxindata.im.view.ChatBitmap;

import java.io.File;


/**
 * Created by Administrator on 2017/10/16.
 * 负责处理聊天界面的图片显示
 */

public class ImageHelper {

    /**
     * 专门处理显示图片到listview上
     *
     * @author:guokailin time:2017/6/30 16:46
     */
    public static void handleImgMessage(final Context context, final ChatMessage message, ChatAdapter.ViewHolder viewHolder, final ChatAdapter adapter) {
        final ChatImageMessageBody chatImageMessageBody = (ChatImageMessageBody) message.getMessageBody();
        final String localPath = chatImageMessageBody.getLocalPath();//本地
        final File localFile = new File(localPath);
        if (localFile.exists()) {//如果本地消息存在
            //使图片显示按照图片宽高比显示
            Glide.with(context)
                    .asBitmap()
                    .load(localPath)
                    .apply(new RequestOptions().override(Util.getWindowWidth(context)/2,Util.getWindowWidth(context)/2))
                    .into(new ChatBitmap(viewHolder.mPictureIv));
            if (message.messageState == ChatMessage.Status.SUCCESS) {
                //如果是自己发送的消息且已经上传就不再上传文件
                viewHolder.mPercentTv.setVisibility(View.GONE);
            }
        } else {
            //下载并显示默认的图片
            viewHolder.mPercentTv.setVisibility(View.GONE);
            viewHolder.mPictureIv.setImageResource(R.drawable.default_image);
            FileHelper.downLoadFileMessage(message, context, adapter);
        }
        //点击处理，无论接收还是发送，如果本地文件丢失，则从服务器获取！
        viewHolder.mPictureIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String localPath = chatImageMessageBody.getLocalPath();//本地
                final File localFile = new File(localPath);
                if (localFile.exists()) {
                    //点击打开
                    FileUtil.openFile(context, new File(localPath));
                } else if (message.messageState == ChatMessage.Status.DOING) {
                    Toast.makeText(context, "正在下载,请稍等", Toast.LENGTH_SHORT).show();
                } else {
                    //点击下载
                    FileHelper.downLoadFileMessage(message, context, adapter);
                }
            }
        });
        viewHolder.mStatusIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                message.messageState = ChatMessage.Status.DOING;
                adapter.notifyDataSetChanged();
                FileHelper.uploadFileMessage(adapter, message);
            }
        });
    }
}
